$(function () {
    getSexByCode();
})
var userCode = sessionStorage.getItem("userCode");
console.log(userCode);

function setName(data) {
    let result = jsonGetValue("/team/getTeamByTeamCode",{teamCode: data.teamCode});
    console.log(result.data);
    if (userCode != null) {
        $("#loginUser").html(data.name);
        $("#teamName").html(result.data.teamName);
        $("#teamCode").html(data.teamCode);
    } else {
        window.location.href = "/html/login.html";
    }
}

// 左侧导航栏页面显示
function go(obj, page) {
    $(".right").load("/html" + page);
}

// 从码表获取性别
function getSexByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"sex"});
    console.log(result.data);
    setSexDataByCode(result.data);
}
// 给性别担心按钮赋值
function setSexDataByCode(data) {
    let html = "";
    for (let i=0;i<data.length;i++){
        html += '<label><input type="radio" name="sex" value="'+ data[i].code +'">'+ data[i].key +'</label>';
    }
    $("#perfectSex").html(html);
    getTeamList();
}

// 获取小组
function getTeamList() {
    let result = jsonGet("/team/getTeamList");
    console.log(result.data);
    setTeamListData(result.data);
}
// 给小组下拉列表赋值
function setTeamListData(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].teamCode +'">'+ data[i].teamName +'</option></div>';
    }
    $("#perfectTeam").html(html);
    getEducationByCode();
}

// 从码表获取学历
function getEducationByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"education"});
    console.log(result.data);
    setEducationDataByCode(result.data);
}
// 给学历下拉列表赋值
function setEducationDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#perfectEducation").html(html);
    findUserByUserCode();
}

// 根据用户编号查询用户
function findUserByUserCode() {
    let result = jsonGetValue("/user/findUserByUserCode",{userCode: userCode});
    console.log(result.data);
    setUserDataByUserCode(result.data);
}
// 获取页面输入的值
function setUserDataByUserCode(data) {
    $("#userCode").val(data.userCode);
    $("#perfectName").val(data.name);
    $("input[name='sex'][value='"+ data.sex +"']").attr("checked",true);
    $("#perfectTeam").find("option[value='"+ data.teamCode +"']").prop("selected",true);
    $("#perfectEducation").find("option[value='"+ data.education +"']").prop("selected",true);
    $("#college").val(data.college);
    $("#address").val(data.address);
    $("#phone").val(data.phone);
    $("#urgentPeople").val(data.urgentPeople);
    setName(data);
}

// 完善信息
function perfect() {
    let data = {
        userCode:userCode,
        name:$.trim($("#perfectName").val()),
        sex:$.trim($("input[name='sex']:checked").val()),
        teamCode:$.trim($("#perfectTeam").val()),
        education:$.trim($("#perfectEducation").val()),
        college:$.trim($("#college").val()),
        address:$.trim($("#address").val()),
        phone:$.trim($("#phone").val()),
        urgentPeople:$.trim($("#urgentPeople").val()),
    }
    let result = jsonPost("/user/perfect", data);
    console.log(result.result)
    if (result.result == 1){
        alert("信息完善成功");
        $(".right").load("/html/user/user-list.html");
    }else if(result.result == -1){
        alert("修改失败");
    }
}

// 退出登录
function loginOut(){
    let result = jsonGet("/user/loginOut");
    console.log(result.result);
    if (result.result == 1){
        window.location.href = "/html/login.html";
    }
}